import { downfileUrl } from "./file.js"
/**
 * 返回上一页，带参不刷新
 */
export function navigateBackParams(params) {
  let pages = getCurrentPages()
  let prevPage = pages[pages.length - 2]; //上一页页面实例
  for(let key in params) {
	  prevPage.$vm[key] = params[key]
  }
  uni.navigateBack({ delta:1 })
}

// url图片分享
export function shareUrlImage(url){
	downfileUrl(url).then((res) => {
		wx.showShareImageMenu({
			path: res.data.tempFilePath
		})
	})
}


// 实际的网络图片地址
export function saveUrlImage(url) {
	uni.showLoading({
		title: '正在保存',
		icon:'none'
	})
	downfileUrl(url).then((res) => {
		if (res.data.statusCode === 200) {
			//鉴权
			uni.authorize({
				scope: 'scope.writePhotosAlbum',
				success: () => {
					//保存图片到相册
					uni.saveImageToPhotosAlbum({
						filePath: res.data.tempFilePath,
						success: () => {
							uni.showToast({
								title: '保存成功'
							})
						},
						fail: (err) => {
							//取消不为错，h5端可能有这个问题
							if (err.errMsg.includes('fail cancel')) {
								uni.hideLoading()
								return
							}
							uni.showToast({
								title: '保存失败',
								icon: "error"
							})
						}
					});
				},
				fail: () => {
					// 鉴权失败手动打开
					uni.hideLoading();
					uni.showModal({
						title: '提示',
						content: '需要保存图片权限，请在设置中打开',
						showCancel: false,
						success: uni.openSetting
					});
				}
			});
		}
	}).catch(() => {
		// 地址有误处理
		uni.showToast({
			title: '图片解析失败',
			icon: 'error'
		})
	})
}